package core

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func NewZap() *zap.SugaredLogger {
	writeSyncer := getLogWriter()
	encoder := getEncoder()
	var level zapcore.Level
	if err := level.Set(C.Logger.Level); err != nil {
		//fmt.Printf("日志等级%#v\n", C.Logger.Level)
		//fmt.Printf("绑定输出日志等级失败%#v\n",err)
		panic(err.Error())

	}
	core := zapcore.NewCore(encoder, writeSyncer, level)

	logger := zap.New(core, zap.AddCaller())
	return logger.Sugar()
}

func getEncoder() zapcore.Encoder {
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	return zapcore.NewConsoleEncoder(encoderConfig)
}

func getLogWriter() zapcore.WriteSyncer {
	lumberJackLogger := &lumberjack.Logger{
		Filename:   C.Logger.Path,
		MaxSize:    C.Logger.Size,
		MaxBackups: C.Logger.Count,
		MaxAge:     C.Logger.Age,
		Compress:   C.Logger.Compress,
	}
	return zapcore.AddSync(lumberJackLogger)
}